﻿
using BaseUtils.Helper;
using BaseUtils.Utils;
using NW_WEB_Service.App.Controllers;
using Service.src.Flow.Server;
using Service.src.Model.Folw;
using Service.src.NW.Model;
using Service.src.NW.Model.Folw;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;

namespace Service.src.Flow.Api
{
    /// <summary>
    /// 流程运行的主类
    /// </summary>
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class FlowRuntimeApi
    {

        /// <summary>
        /// 添加流程
        /// </summary>
        /// <returns></returns>
        public string AddFlow()
        {
            List<FlowBaseInfo> nws = AppData.AllFlowBaseInfos.Values.ToList();
            FlowBaseInfo Item = nws.OrderByDescending(s => s.id).FirstOrDefault() ?? new FlowBaseInfo();
            long Index = Item.id + 1;
            string Path = "NEW_" + Index;
            FlowBaseInfo _new = new FlowBaseInfo();
            _new.id = Index;
            _new.Title = Path;
            _new.FileName = "index.nw";
            _new.FilePath = AppData.RuntimeFlowDataFlowDirectory + Path;
            _new.OutTime = 10000;

            AppData.AllFlowBaseInfos.TryAdd(  _new.id,_new);

            FileHelper.WriteContent($"{AppData.RuntimeFlowData}", AppData.AllFlowFileName, AppData.AllFlowBaseInfos.Values.ToList().ToJson());
            FlowRoot Folw = new FlowRoot();
            Folw.name = _new.Title;
            Folw.Id = _new.id;
            FlowProcessor.UpdateFlow(Folw, false);
            new FlowController().GetFlowList();
            AppData.AddOpenFlow(Folw);
            return _new.ToJson();
        }

        /// <summary>
        /// 更新流程名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        //public string UpdateFlow(int id, string Title)
        //public string UpdateFlow(string Info)
        //{
        //    FlowBaseInfo NewFlowInfo = Info.ToObject<FlowBaseInfo>();
        //    //List<FlowBaseInfo> nws = AppData.AllFlowBaseInfos;
        //    FlowBaseInfo Old = AppData.AllFlowBaseInfos[NewFlowInfo.id]??new FlowBaseInfo();
        //    Old.Title = NewFlowInfo.Title;
        //    Old.EnableCycle = NewFlowInfo.EnableCycle;
        //    Old.CycleTime = NewFlowInfo.CycleTime;
        //    Old.OutTime = NewFlowInfo.OutTime;

        //    string FlowJson = AppData.AllFlowBaseInfos.Values.ToList().ToJson();
        //    FileHelper.WriteContent($"{AppData.RuntimeFlowData}", AppData.AllFlowFileName, FlowJson);
        //    return FlowJson;
        //}
        /// <summary>
        /// 删除流程
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        //public string DeleteFlow(long Id)
        //{
        //    //List<FlowBaseInfo> nws = AppData.AllFlowBaseInfos.Where(s => s.id != Id).ToList();
        //    FlowBaseInfo RemoveFlow;
        //    AppData.AllFlowBaseInfos.TryRemove(Id, out RemoveFlow);
        //    List<FlowBaseInfo> nws = AppData.AllFlowBaseInfos.Values.ToList();
        //    FileHelper.WriteContent(AppData.RuntimeFlowData, $"{AppData.AllFlowFileName}", nws.ToJson());

        //    return GetAllFlowFile();
        //}

        /// <summary>
        /// 根据流程id获取流程信息
        /// </summary>
        /// <param name="FlowId"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        //public string GetFlowByFlowId(long FlowId)
        //{
        //    if (FlowId <= 0)
        //    {
        //        throw new Exception("流程ID错误，请检测流程");
        //    }
        //    FlowRoot Flow = FlowProcessor.GetFlow(FlowId);
        //    //GlobalParams.AddOpenFlow(Flow);
        //    return Flow.ToJson();
        //}

        /// <summary>
        /// 获取所有流程文件
        /// </summary>
        /// <returns></returns>
        //public string GetAllFlowFile()
        //{
        //    string Flows = AppData.RuntimeFlowData + AppData.AllFlowFileName;
        //    if (!File.Exists(Flows))
        //    {
        //        return null;
        //    }

        //    string nws = FileHelper.ReadContent(Flows);
        //    //AppData.SetAllFlows(nws);
        //    return nws;
        //}

        /// <summary>
        /// 根据流程ID， 执行流程
        /// </summary>
        /// <param name="FlowId"></param>
        /// <exception cref="Exception"></exception>
        //public void RunFlow(long FlowId)
        //{
        //    if (FlowId <= 0)
        //    {
        //        throw new Exception("流程ID错误，请检测流程");
        //    }
        //    FlowServer.RunFlow(FlowId);
        //}

        /// <summary>
        /// 停止某个流程
        /// </summary>
        /// <param name="FlowId"></param>
        /// <exception cref="Exception"></exception>
        //public string StopFlow(long FlowId)
        //{
        //    if (FlowId <= 0)
        //    {
        //        throw new Exception("流程ID错误，请检测流程");
        //    }
        //    bool IsStop = FlowServer.StopFlow(FlowId);
        //    return IsStop.ToString();
        //}

    }
}
